package com.sky.mapper;

import com.sky.entity.ShoppingCart;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;

@Mapper
public interface ShoppingCartMapper {

    /**
     * 根据菜品ID获得购物车信息
     * @return
     */
    @Select("select  * from shopping_cart where dish_id = #{dishId} and user_id = #{userId}")
    ShoppingCart getByDishId(Long dishId, Long userId);

    /**
     * 菜品数量自增+1
     * @param dishId
     * @param userId
     */
    @Update("update shopping_cart set number = number + 1,amount = amount + amount where user_id = #{userId} and dish_id = #{dishId}")
    void increaseDish(Long dishId, Long userId);

    /**
     * 添加商品（套餐或者菜品）
     * @param shoppingCart
     */
    void add(ShoppingCart shoppingCart);

    /**
     * 根据套餐ID获得购物车信息
     * @param setmealId
     * @param userId
     * @return
     */
    @Select("select * from shopping_cart where setmeal_id = #{setmealId} and user_id = #{userId}")
    ShoppingCart getBySetMealId(Long setmealId, Long userId);

    /**
     * 套餐数量自增+1
     * @param setmealId
     * @param userId
     */
    @Update("update shopping_cart set number = number + 1,amount = amount + amount " +
            "where setmeal_id = #{setmealId} and user_id = #{userId}")
    void increaseSetMeal(Long setmealId, Long userId);

    /**
     * 购物车列表查询
     * @param userId
     * @return
     */
    @Select("select  * from shopping_cart where user_id = #{userId}")
    List<ShoppingCart> list(Long userId);

    /**
     * 套餐数量自减-1
     *
     * @param dishId
     * @param setmealId
     * @param price
     * @param userId
     */
    void sub(Long dishId, Long setmealId, BigDecimal price, Long userId);

    /**
     * 删除套餐
     * @param dishId
     * @param setmealId
     * @param userId
     */
    void del(Long dishId, Long setmealId, Long userId);

    /**
     * 清空购物车
     */
    @Delete("delete from shopping_cart where user_id = #{userId}")
    void clean(Long userId);
}
